package com.company.multidimensiontest;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

import java.util.Queue;

// TODO : add scale gesture event to csr !!!!

public class MultiDimensionTest extends Activity {
    /** Called when the activity is first created. */
    
    private LinearLayout ll;
    private ClearSurfacceView csr;
    
    private ScaleGestureDetector mScaleGestureDetector;
    private float mScaleFactor = 100.f;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        mScaleGestureDetector = new ScaleGestureDetector(this, new ScaleListener());
        
        /* use my GLSurfaceView and my Renderer */
        
        csr = new ClearSurfacceView(this);
        ll = (LinearLayout) findViewById(R.id.LinearLayout01);
        
        
        
        /* use android basic lib's GLSurfaceView and add my Renderer */
        /*
        GLSurfaceView sv = (GLSurfaceView) findViewById(R.id.surface_view);
        sv.setRenderer(new ClearRenderer());
        */
        
        /* put 2D layout over 3D surface view */
        /* 
        ClearSurfacceView sv = new ClearSurfacceView(this);                
        setContentView(sv);
        
        View v = getLayoutInflater().inflate(R.layout.main, null);
        addContentView(v, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        */
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        ll.addView(csr, (int) mScaleFactor, (int) mScaleFactor);        
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        
        mScaleGestureDetector.onTouchEvent(event);
        
        return true;
    }
    
    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener{

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            mScaleFactor *= detector.getScaleFactor();
            mScaleFactor = Math.max(50.f, Math.min(mScaleFactor, 480.0f));
            drawCSR();
            return true;
        }
    }
    
    private void drawCSR(){                
        if(ll.getChildAt(2) == csr){            
            csr.layout(csr.getLeft(), csr.getTop(),csr.getLeft() + (int)mScaleFactor, csr.getTop() + (int)mScaleFactor);
        }
    }

}